rm(list = ls())

library(data.table)
library(estimatr)
library(texreg)

data_pooled <- readRDS('./replication_hasz/output/data/data_pooled.rds')

## OLS tables for appendix
data <- data_pooled[study=='Pooled']
fldata <- data_pooled[study=='Florida Study']
fdata <- data_pooled[study=='U.S. Study']

# Pooled
data[complete.cases(data[, c('college', 'male', 'white', 'latino', 'republican',
                             'strong_partisan', 'work', 'w_partner',
                             'att_mig', 'native_born', 'interest_politics' )]), complete:=1]
data[is.na(complete), complete:=0]

out1i <- lm_robust(vote_usc ~ t2 + t3 + college + male + white + latino + republican  + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=data)
out2i <- lm_robust(vote_ownc ~ t2 + t3 + college + male + white + latino + republican  + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=data)
out3i <- lm_robust(ballotc ~ t2 + t3 + college + male + white + latino + republican  + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=data)
out4i <- lm_robust(more_infoc ~ t2 + t3 + college + male + white + latino + republican  + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=data)

out1 <- lm_robust(vote_usc ~ t2 + t3,
                  data=data, subset = complete==1)
out2 <- lm_robust(vote_ownc ~ t2 + t3,
                  data=data, subset = complete==1)
out3 <- lm_robust(ballotc ~ t2 + t3,
                  data=data, subset = complete==1)
out4 <- lm_robust(more_infoc ~ t2 + t3,
                  data=data, subset = complete==1)

texreg::texreg(list(out1, out2, out3, out4, out1i, out2i, out3i, out4i),
               file = './replication_hasz/output/tables/tabD1.tex',
               stars = c(0.001,0.01,0.05),
               custom.header = list('vote US' = 1, 'vote own' = 2, 'gov dec' = 3, 'more info' = 4,
                                    'vote US' = 5, 'vote own' = 6, 'gov dec' = 7, 'more info' = 8),
               custom.model.names = c('(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)'),
               custom.coef.map = list('(Intercept)' = 'control', 't2' = 'co-partisan', 't3' = 'counter-partisan',
                                      'college' = 'college', 'male' = 'male', 'white' = 'white', 'latino' = 'latino',
                                      'republican' = 'republican',
                                      'strong_partisan' = 'strong partisan', 'work' = 'employed',
                                      'w_partner' = 'married/partner', 'att_mig' = 'attitudes migrants',
                                      'native_born' = 'native born', 'interest_politics' = 'interest politics'),
               custom.gof.rows = list(cov = rep(c('no', 'yes'), each=4)),
               ci.force = FALSE,
               digits = 3,
               caption = "Treatment effects on support for non-citizen local voting rights in Pooled sample",
               caption.above = TRUE,
               threeparttable = TRUE,
               include.rsquared = FALSE, include.adjrs = TRUE, include.nobs = TRUE, include.rmse = FALSE,
               label = "table:ols_pooled",
               custom.note = "\\item Presents estimates of average treatment effects and heteroskedasticity-consistent 95% CIs without and with covariate adjustment for the Pooled sample (pooling together Study 1 and Study 2). The sample includes Democratic, Republican and Independent (for Study 1 only) registered voters who answered covariate and outcome questions. \n %stars")


# Study 1
fldata[complete.cases(fldata[, c('college', 'male', 'white', 'latino', 'republican',
                                 'independent', 'strong_partisan', 'work', 'w_partner',
                                 'att_mig', 'native_born', 'interest_politics' )]), complete:=1]
fldata[is.na(complete), complete:=0]

out1i <- lm_robust(vote_usc ~ t2 + t3 + college + male + white + latino + republican + independent + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fldata)
out2i <- lm_robust(vote_ownc ~ t2 + t3 + college + male + white + latino + republican + independent + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fldata)
out3i <- lm_robust(ballotc ~ t2 + t3 + college + male + white + latino + republican + independent + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fldata)
out4i <- lm_robust(more_infoc ~ t2 + t3 + college + male + white + latino + republican + independent + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fldata)


out1 <- lm_robust(vote_usc ~ t2 + t3,
                  data=fldata, subset = complete==1)
out2 <- lm_robust(vote_ownc ~ t2 + t3,
                  data=fldata, subset = complete==1)
out3 <- lm_robust(ballotc ~ t2 + t3,
                  data=fldata, subset = complete==1)
out4 <- lm_robust(more_infoc ~ t2 + t3,
                  data=fldata, subset = complete==1)

texreg::texreg(list(out1, out2, out3, out4, out1i, out2i, out3i, out4i),
               file = './replication_hasz/output/tables/tabD2.tex',
               stars = c(0.001,0.01,0.05),
               custom.header = list('vote US' = 1, 'vote own' = 2, 'gov dec' = 3, 'more info' = 4,
                                    'vote US' = 5, 'vote own' = 6, 'gov dec' = 7, 'more info' = 8),
               custom.model.names = c('(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)'),
               custom.coef.map = list('(Intercept)' = 'control', 't2' = 'co-partisan', 't3' = 'counter-partisan',
                                      'college' = 'college', 'male' = 'male', 'white' = 'white', 'latino' = 'latino',
                                      'republican' = 'republican', 'independent' = 'independent',
                                      'strong_partisan' = 'strong partisan', 'work' = 'employed',
                                      'w_partner' = 'married/partner', 'att_mig' = 'attitudes migrants',
                                      'native_born' = 'native born', 'interest_politics' = 'interest politics'),
               custom.gof.rows = list(cov = rep(c('no', 'yes'), each=4)),
               ci.force = FALSE,
               digits = 3,
               caption = "Treatment effects on support for non-citizen local voting rights in Study 1",
               caption.above = TRUE,
               threeparttable = TRUE,
               include.rsquared = FALSE, include.adjrs = TRUE, include.nobs = TRUE, include.rmse = FALSE,
               label = "table:ols_study1",
               custom.note = "\\item Presents estimates of average treatment effects and heteroskedasticity-consistent 95\\% CIs without and with covariate adjustment for Study 1. The sample includes Democratic, Republican and Independent registered voters who answered covariate and outcome questions. \n %stars")


# Study 2
fdata[complete.cases(fdata[, c('college', 'male', 'white', 'latino', 'republican',
                               'strong_partisan', 'work', 'w_partner',
                               'att_mig', 'native_born', 'interest_politics' )]), complete:=1]
fdata[is.na(complete), complete:=0]

out1i <- lm_robust(vote_usc ~ t2 + t3 + college + male + white + latino + republican  + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fdata)
out2i <- lm_robust(vote_ownc ~ t2 + t3 + college + male + white + latino + republican  + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fdata)
out3i <- lm_robust(ballotc ~ t2 + t3 + college + male + white + latino + republican  + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fdata)
out4i <- lm_robust(more_infoc ~ t2 + t3 + college + male + white + latino + republican  + strong_partisan + work + w_partner + att_mig + native_born + interest_politics,
                   data=fdata)

out1 <- lm_robust(vote_usc ~ t2 + t3,
                  data=fdata, subset = complete==1)
out2 <- lm_robust(vote_ownc ~ t2 + t3,
                  data=fdata, subset = complete==1)
out3 <- lm_robust(ballotc ~ t2 + t3,
                  data=fdata, subset = complete==1)
out4 <- lm_robust(more_infoc ~ t2 + t3,
                  data=fdata, subset = complete==1)

texreg::texreg(list(out1, out2, out3, out4, out1i, out2i, out3i, out4i),
               file = './replication_hasz/output/tables/tabD3.tex',
               stars = c(0.001,0.01,0.05),
               custom.header = list('vote US' = 1, 'vote own' = 2, 'gov dec' = 3, 'more info' = 4,
                                    'vote US' = 5, 'vote own' = 6, 'gov dec' = 7, 'more info' = 8),
               custom.model.names = c('(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)'),
               custom.coef.map = list('(Intercept)' = 'control', 't2' = 'co-partisan', 't3' = 'counter-partisan',
                                      'college' = 'college', 'male' = 'male', 'white' = 'white', 'latino' = 'latino',
                                      'republican' = 'republican',
                                      'strong_partisan' = 'strong partisan', 'work' = 'employed',
                                      'w_partner' = 'married/partner', 'att_mig' = 'attitudes migrants',
                                      'native_born' = 'native born', 'interest_politics' = 'interest politics'),
               custom.gof.rows = list(cov = rep(c('no', 'yes'), each=4)),
               ci.force = FALSE,
               digits = 3,
               caption = "Treatment effects on support for non-citizen local voting rights in Study 2",
               caption.above = TRUE,
               threeparttable = TRUE,
               include.rsquared = FALSE, include.adjrs = TRUE, include.nobs = TRUE, include.rmse = FALSE,
               label = "table:ols_study2",
               custom.note = "\\item Presents estimates of average treatment effects and heteroskedasticity-consistent 95% CIs without and with covariate adjustment for Study 2. The sample includes Democratic and Republican registered voters who answered covariate and outcome questions. \n %stars")


